<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>登录</title>

    <link rel="stylesheet" href="loginStyle.css" />
  </head>
  <body>
    <main class="mainArea">
      <div class="nameInput">
        <span>用户名：</span>
        <input type="text" name="name" />
      </div>
      <div class="passInput">
        <span>密码：</span>
        <input type="password" name="pas" />
      </div>
      <div class="btnArea">
        <button>注册</button>
        <button id="logBtn">登录</button>
      </div>
    </main>

    <script>
      let loginBtn = document.querySelector("#logBtn");
      let Ninput = document.querySelector(".nameInput input");
      let Pinput = document.querySelector(".passInput input");
      loginBtn.addEventListener("click", (evt) => {
        let dataToPost = JSON.stringify({
          userName: Ninput.value,
          password: Pinput.value,
        });

        const options = {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: dataToPost,
        };
        fetch("http://localhost:3000/login", options)
          .then((res) => {
            return res.json();
          })
          .then((res) => {
            if(res.ok === 1){
              location.href = "http://localhost:3000/home.html"
              
            }
          })
          .catch((err) => {
            console.log(err);
          });
      });
    </script>
  </body>
</html>
